package flc.ast.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.StatFs;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.activity.MusicActivity;
import flc.ast.activity.PictureActivity;
import flc.ast.activity.SearchFileActivity;
import flc.ast.activity.VideoActivity;
import flc.ast.activity.WordActivity;
import flc.ast.databinding.FragmentFileBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.device.StorageUtil;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FileFragment extends BaseNoModelFragment<FragmentFileBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_file;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivFileSearch.setOnClickListener(this);
        mDataBinding.ivVideo.setOnClickListener(this);
        mDataBinding.ivMusic.setOnClickListener(this);
        mDataBinding.ivPicture.setOnClickListener(this);
        mDataBinding.ivWord.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivFileSearch:
                String searchName=mDataBinding.etFileName.getText().toString().trim();
                if (searchName.isEmpty()){
                    ToastUtils.showShort(getString(R.string.please_input_search));
                    return;
                }
                SearchFileActivity.searchName=searchName;
                startActivity(SearchFileActivity.class);
                break;
            case R.id.ivVideo:
                startActivity(VideoActivity.class);
                break;
            case R.id.ivWord:
                startActivity(WordActivity.class);
                break;
            case R.id.ivPicture:
                startActivity(PictureActivity.class);
                break;
            case R.id.ivMusic:
                startActivity(MusicActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        StatFs statFs = StorageUtil.getInternalMemory();//内部存储
        //已使用
        long usedLong = statFs.getTotalBytes() - statFs.getAvailableBytes();
        //总内存
        long allLong = statFs.getTotalBytes();
        //已用百分比
        int progress = (int) ((statFs.getTotalBytes() - statFs.getAvailableBytes()) * 100f / statFs.getTotalBytes());
        mDataBinding.tvUsed.setText(getString(R.string.all_storage) + ConvertUtils.byte2FitMemorySize(usedLong, 0)+"/");
        mDataBinding.tvAll.setText(ConvertUtils.byte2FitMemorySize(allLong, 0));
        mDataBinding.tvUsedPercent.setText(progress + getString(R.string.unit_percent));
        mDataBinding.pbUsed.setProgress(progress);
    }
}